import React, { useState, useEffect, Fragment } from 'react';
import { connect } from 'umi';
import { message, Cascader } from 'antd';
// 登录信息
import { getAuthority } from '@/utils/authority';
const loginInfo = getAuthority();

const OrgSelect = (props: {
  dispatch?: any;
  onChange?: (checkedList: any[]) => void;
  value?: any;
  disabled?: boolean;
}) => {
  const { dispatch, disabled, value } = props;

  // 是否加载完成
  const [isLoaded, setIsLoaded] = useState(false);

  // 组织列表
  const [optionOrgList, setOptionOrgList] = useState<any>([]);


  // 初始进来加载一次
  useEffect(() => {
    if (loginInfo.currentRoleId === '1') {
      setOptionOrgList([{
        label: '伯乐寻骥官方平台',
        value: loginInfo.orgId,
        type: loginInfo.currentRoleId,
        isLeaf: false,
      }])
    } else {
      setOptionOrgList([{
        label: loginInfo.maxSchOrgName,
        value: loginInfo.maxSchOrgId,
        type: loginInfo.currentRoleId,
        isLeaf: false,
      }])
    }
  }, []);

  // 如果有默认值再重新加载一次
  useEffect(() => {
    if (value && !isLoaded) {

    }
  }, [value]);

  const onChange = (value: any, option: any) => {
    console.log(value, option, 'onChange');
    // 加载完成
    setIsLoaded(true);
    // value 返回值是一个数组
    props.onChange?.(value);
  };

  const loadData = async (selectedOptions: any) => {
    const targetOption = selectedOptions[selectedOptions.length - 1];
    targetOption.loading = true;
    switch (targetOption.type) {
      case '1':
        //平台
        dispatch({
          type: 'common/getSchoolList',
          payload: {},
          callback: (res: any) => {
            const { code, data, msg } = res
            if (code === 1) {
              targetOption.loading = false;
              targetOption.children = data.map((v: any) => ({ label: v.name, value: v.id, type: '3', isLeaf: false, }));
              setOptionOrgList([...optionOrgList]);
            } else {
              message.error(msg)
            }
          }
        });
        break;
      case '3':
        //学校
        dispatch({
          type: 'common/getDepartmentList',
          payload: { id: targetOption.value },
          callback: (res: any) => {
            const { code, data, msg } = res
            if (code === 1) {
              targetOption.loading = false;
              targetOption.children = data.map((v: any) => ({ label: v.name, value: v.id, type: '6', isLeaf: false, }));
              setOptionOrgList([...optionOrgList]);
            } else {
              message.error(msg)
            }
          }
        });
        break;
      case '6':
        //院系
        dispatch({
          type: 'common/findMajorIdAndNameListByDeptId',
          payload: { id: targetOption.value },
          callback: (res: any) => {
            const { code, data, msg } = res
            if (code === 1) {
              targetOption.loading = false;
              targetOption.children = data.map((v: any) => ({ label: v.name, value: v.id, type: '7' }));
              setOptionOrgList([...optionOrgList]);
            } else {
              message.error(msg)
            }
          }
        });
      default:
        break;
    }
  };

  return (
    <Cascader
      value={value}
      options={optionOrgList}
      loadData={loadData}
      onChange={onChange}
      changeOnSelect
      disabled={disabled}
    />
  );
};
export default connect()(OrgSelect);
